#include "Screens\HomeScreen.h"
#include "Screens\GameScreen.h"
#include "Screens\LevelScreen.h"
#include "Objects/Objects.h"
#include "SFML/Network.hpp"

HomeScreen::HomeScreen(GameState *gameState) : ScreenBase(gameState){
	m_text = "Connect to the internet to receive updates!";
}


HomeScreen::~HomeScreen(){
	clean();
}


void HomeScreen::init(){
	ScreenBase::init();

	sf::Http http;
	http.setHost("http://www.jgcamarasa.com/");

	sf::Http::Request request;
	request.setMethod(sf::Http::Request::Get);
	request.setUri("/Tristesa/homeText.txt");
	request.setHttpVersion(1, 1); // HTTP 1.1
	request.setField("From", "me");
	request.setField("Content-Type", "text/plain");
	

	sf::Http::Response response = http.sendRequest(request);

	if(!response.getBody().empty()){
		m_text = response.getBody();
	}
	
	m_gameState->camera.posX = m_gameState->camera.posY = 0;

	Background *background = new Background(m_gameState, "homeBackground.png");
	addObject(background, 9);

	HomeFire *homeFire = new HomeFire(m_gameState);
	homeFire->setPosition(b2Vec2(-20.0f, -15.75f));
	homeFire->setSize(b2Vec2(48.0f, 48.0f));
	addObject(homeFire,0);

	GameObject *logo = new GameObject(m_gameState);
	logo->setTexture(*(m_gameState->getTextureManager().getTexture("logo.png")));
	logo->setPosition(b2Vec2(0.0f, 7.0f));
	logo->setSize(b2Vec2(40,25));
	addObject(logo, 0);

	IntermitentText *text = new IntermitentText(m_gameState);
	text->setText(m_text);
	text->setColor(0.8f, 0.8f, 0.1f);
	text->setAlignment(CENTER);
	text->setFontSize(30);
	text->setPosition(b2Vec2(0.0f, -2.0f));
	addObject(text, 0);

	m_music.openFromFile(soundsPath+"GoodLuck.ogg");
	m_music.setLoop(true);
	m_music.play();

}


void HomeScreen::update(double dt){
	ScreenBase::update(dt);

	static bool prevActionPress = false;

	bool actionPress = Keyboard::isKeyPressed(Keyboard::Space) || Joystick::isButtonPressed(0, 0);

	if(!actionPress && prevActionPress){
		m_gameState->setCurrentScreen(new LevelScreen(m_gameState));
	}

	prevActionPress = actionPress;
}


void HomeScreen::draw(){
	ScreenBase::draw();
}